﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FastShip88
{
    public partial class UserAddress : System.Web.UI.Page
    {
        DBAccess.UsersDataContext db;

        public int ADDR_ID
        {
            get { return SafeConvert.ToInt(ViewState["ADDR_ID"]); }
            set { ViewState["ADDR_ID"] = value; }
        }

        public bool IS_EDIT
        {
            get { return SafeConvert.ToBool(ViewState["IS_EDIT"]); }
            set { ViewState["IS_EDIT"] = value; }
        }

        private int userId;
        public DBAccess.UserAddress userAddress;
        public string province;
        public string city;
        public string area;
        public string street;
        

        protected void Page_Load(object sender, EventArgs e)
        {
            db = new DBAccess.UsersDataContext();
            userId = Utility.GetUserId(this);
            userAddress = new FastShip88.DBAccess.UserAddress();

            if(SafeConvert.ToBool( ViewState["IS_EDIT"]) ) 
                btnSubmit.Text = "修改"; 
            else 
                btnSubmit.Text = "添加";

            if (!Page.IsPostBack)
            {
                InitGridView();
            }


        }

        private void InitGridView()
        {
            var qs = from ua in db.UserAddresses
                     where ua.UserId == userId
                     select ua;

            foreach (var q in qs)
            {
                if (q.Address.Trim().Length > 0)
                {
                    string[] address = q.Address.Split(new char[] { '@' });
                    if (address.Length >= 2)
                    {
                        if (address[0].EndsWith("市"))
                        {
                            q.Address = address[0] + address[2] + address[3];
                        }
                        else
                        {
                            q.Address = address[0] + address[1] + address[2] + address[3];
                        }
                    }
                }

            }
            gvAddrList.DataSourceID = null;
            gvAddrList.DataSource = qs;
            gvAddrList.DataBind();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            if (userId <= 0)
            {
                string scriptString = "<script language=JavaScript> alert(\"";
                scriptString += "请先维护用户资料！";
                scriptString += "\");</script> ";
                if (!IsClientScriptBlockRegistered("clientScript"))
                {
                    RegisterClientScriptBlock("clientScript", scriptString);
                }

                return;
            }

            string receiptor = Request.Form.Get("receiptor").Trim();
            string province = Request.Form.Get("province").Trim();
            string city = Request.Form.Get("city").Trim();
            string area = Request.Form.Get("area").Trim();
            string street = Request.Form.Get("street").Trim();
            string postcode = Request.Form.Get("postcode").Trim();
            string phone = Request.Form.Get("phone").Trim();
            string mobile = Request.Form.Get("mobile").Trim();

            string address = province + "@" + city + "@" + area + "@" + street;

            if (IS_EDIT)
            {
                var q = (from ua in db.UserAddresses
                         where ua.id == ADDR_ID
                         select ua).First();

                q.receiptor = receiptor;
                q.Address = address;
                q.PostCode = postcode;
                q.Phone = phone;
                q.Mobile = mobile;

                db.SubmitChanges();
                IS_EDIT = false;
                btnSubmit.Text = "添加";

            }
            else
            {
                var useraddr = new DBAccess.UserAddress
                {
                    UserId = userId,
                    receiptor = receiptor,
                    Address = address,
                    PostCode = postcode,
                    Phone = phone,
                    Mobile = mobile
                };

                db.UserAddresses.InsertOnSubmit(useraddr);
                db.SubmitChanges();
            }

            InitGridView();
            
            province = ""; city = ""; area = ""; street = "";


        }

  


    
        protected void gvAddrList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DataKey key = gvAddrList.DataKeys[e.RowIndex];
            int addr_id = SafeConvert.ToInt(key.Value.ToString());

            var q = (from ua in db.UserAddresses
                     where ua.id == addr_id
                     select ua).First();
            db.UserAddresses.DeleteOnSubmit(q);
            db.SubmitChanges();

            InitGridView();
            IS_EDIT = false;
            btnSubmit.Text = "添加";

        }

        protected void gvAddrList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            DataKey key = gvAddrList.DataKeys[e.NewEditIndex];
            int addr_id = SafeConvert.ToInt(key.Value.ToString());

            var q = (from ua in db.UserAddresses
                     where ua.id == addr_id
                     select ua).First();

            userAddress = q;
            userAddress.receiptor = userAddress.receiptor.Trim();
            userAddress.Mobile = userAddress.Mobile.Trim();
            userAddress.Phone = userAddress.Phone.Trim();

            string[] address = q.Address.Split(new char[] { '@' });
            province= address[0].Trim();
            city=  address[1].Trim();
            area=address[2].Trim();
            street=address[3].Trim();

            ADDR_ID = addr_id;
            IS_EDIT = true;
            btnSubmit.Text = "修改";

        }

        protected void gvAddrList_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            DataKey key = gvAddrList.DataKeys[e.NewSelectedIndex];
            int addr_id = SafeConvert.ToInt(key.Value.ToString());

            var qs = from ua in db.UserAddresses
                     where ua.UserId == userId
                     select ua;

            foreach (var q in qs)
            {
                if (q.id == addr_id)
                {
                    q.IsDefault = true;
                }
                else
                {
                    q.IsDefault = false;
                }

            }
            db.SubmitChanges();

            InitGridView();
 

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            IS_EDIT = false;
            btnSubmit.Text = "添加";
            province = ""; city = ""; area = ""; street = "";

        }

   

   
    }
}
